"""
This fle will be used t creat to train and testdata  using prml database
"""

import os
import MySQLdb

table_type = "posts"
file_path = "D:\JIIT\PRML\project\dataset\Posts"
database = "prml"
user = "root"
password="123"
port=3306
table = "posts"
train_all_dir = "D:\\JIIT\\PRML\\project\\trainall\\"
test_all_dir = "D:\\JIIT\\PRML\\project\\testall\\"

train_title_dir = "D:\\JIIT\\PRML\\project\\train_title\\"
test_title_dir = "D:\\JIIT\\PRML\\project\\test_title\\"


train_body_dir = "D:\\JIIT\\PRML\\project\\train_body\\"
test_body_dir = "D:\\JIIT\\PRML\\project\\test_body\\"


db1= MySQLdb.connect(host="localhost", user=user, passwd=password,db=database, port=port)
select_cursor = db1.cursor()

str1 = "select tags from posts where  posttypeid=1"
select_cursor.execute(str1)

db_tags = select_cursor.fetchall()

all_tags= []
for t in db_tags:
    #print t
    val1 = t[0].split(">")
    #print "val1=", val1
    for temp in val1:
        if temp!='':
            val2  =(temp.split("<"))[1]
            #print "val2=",val2
            
            all_tags.append(val2)
           
           
print "all_tags=", all_tags 
all_tags.sort()

for val in all_tags:
    count = all_tags.count(val)
    #print "val=", val, "count=", count            
            
                

filter_tags = []
for val in all_tags:
    count =all_tags.count(val)
    if count >2:
        even = count %2
        if even ==0:
            print "val=", val, "count=", count
            try:
                filter_tags.index(val)
            except:
                filter_tags.append(val)
                

for val in filter_tags:
    #create_directories()
    if not os.path.exists(train_all_dir+"\\"+val):os.makedirs(train_all_dir+"\\"+val)
    if not os.path.exists(test_all_dir+"\\"+val):os.makedirs(test_all_dir+"\\"+val)
    
    if not os.path.exists(train_title_dir+"\\"+val):os.makedirs(train_title_dir+"\\"+val)
    if not os.path.exists(test_title_dir+"\\"+val):os.makedirs(test_title_dir+"\\"+val)
    
    if not os.path.exists(train_body_dir+"\\"+val):os.makedirs(train_body_dir+"\\"+val)
    if not os.path.exists(test_body_dir+"\\"+val):os.makedirs(test_body_dir+"\\"+val)
    
    
for val in filter_tags: 
                  
    str2  = "select id,title, body from posts where tags like '%<"+val+">%' and posttypeid = 1"
    print str2
    select_cursor.execute(str2)
    result  = select_cursor.fetchall()
    total_tuple= len(result)
    half  = total_tuple/2
    count = 0
    
    for row in result:
        count=count+1
        print "row=", row
        id = row[0]
        title = row[1]
        body = row[2]
        
        print "id= ", id, "title=", title, "body=", body
        
        """create file in traing and testing"""
        if count <= half:
            file_id = (str)(id)
            train_all_file_path = train_all_dir+"\\"+val+"\\"+file_id+".txt"
            print "train all file path=", train_all_file_path
            train_all_file = open(train_all_file_path,'w')
            train_all_file.write(title+"\n"+body)
            train_all_file.close()
            
            
            train_title_file_path = train_title_dir+"\\"+val+"\\"+file_id+".txt"
            train_title_file = open(train_title_file_path,'w')
            train_title_file.write(title)
            train_title_file.close()
            
              
            train_body_file_path = train_body_dir+"\\"+val+"\\"+file_id+".txt"
            train_body_file = open(train_body_file_path,'w')
            train_body_file.write(body)
            train_body_file.close()
            
        else:   
        
            file_id = (str)(id)
            test_all_file_path = test_all_dir+"\\"+val+"\\"+file_id+".txt"
            print "test all file path=", test_all_file_path
            test_all_file = open(test_all_file_path,'w')
            test_all_file.write(title+"\n"+body)
            test_all_file.close()
            
            
            test_title_file_path = test_title_dir+"\\"+val+"\\"+file_id+".txt"
            test_title_file = open(test_title_file_path,'w')
            test_title_file.write(title)
            test_title_file.close()
            
              
            test_body_file_path = test_body_dir+"\\"+val+"\\"+file_id+".txt"
            test_body_file = open(test_body_file_path,'w')
            test_body_file.write(body)
            test_body_file.close()
    #create_file()
    #file_path = directory_path +"\\"+val+""
    
    


